package cn.edu.zut.cs.Business;

import cn.edu.zut.cs.DAO.ReaderDAO;
import cn.edu.zut.cs.Model.Reader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import java.util.List;

@Service
public class readerService<Readers> {
    ReaderDAO readerDAO;
    @Autowired
    public void setReaderDAO(ReaderDAO readerDAO){
        this.readerDAO=readerDAO;
    }
    //增加读者信息
    public void insert_reader(Reader reader){
        readerDAO.insertReader(reader);
    }
    //删除读者信息
    public void deletreader(Reader reader,String id) { readerDAO.deleteById(id); }
    //修改读者信息
   public void updatereader(Reader reader,String id,String pa,String na,String nu)
   {
       reader.setID(id);
       reader.setRpassword(pa);
       reader.setName(na);
       reader.setNum(nu);
       readerDAO.modifyReader(reader);
   }

@Transactional
    public int bianli(String ID, String password){
        List<Reader> ReaderList=ReaderDAO.queryAll();
        for(Reader r:ReaderList) {
//            r.getID();
//            r.getRpassword();
            if (r.getID().equals(ID) && r.getRpassword().equals(password)) {
                return 1;
            }else{
                return 2;
            }
        }
        return 0;
    }

    //   public String updatereader(Model model, Reader reader) {
  //      System.out.println(raeder);
  //      readerDAO.modifyReader(reader);
  //      Readers readers=readerDAO.queryOneReaderById(reader.getID())
  //      Books books = bookService.queryBookById(book.getBookID());
  //      model.addAttribute("books", books);
  //      return "redirect:/book/allBook";
 //   }
//    readerService reader_Service;.
//    public readerService getInstance(){
//        if(reader_Service==null){
//            synchronized (readerService.class){
//                if(reader_Service==null){
//                    reader_Service=new readerService();
//                }
//            }
//        }
//        return reader_Service;
//    }


    //public void deleteById(String id){readerDAO.deleteById(id);}
    // public void modifyReader(Reader reader) { readerDAO.modifyReader(reader); }
//    public Reader getIDandrpassword(String ID,String password){
//        String sql="select * from Reader where ID= ? and password=?";
//        try(
//            Connection conn=mysqltool.connection();
//            PreparedStatement ps=conn.prepareStatement(sql);
//
//            ps.setString(1,ID);
//            ps.setString(2,password);
//            ResultSet rs=ps.executeQuery();
//            if() {
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }

}
